<?php

namespace app\home\model\page\order;

use app\api\model\dao\OrderInfo;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;

class UpdateOrderShipping extends PageBase
{
    private $obj;

    public function __construct()
    {
        parent::__construct();
        $this->obj = new OrderInfo();
    }

    public function excute($param)
    {
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }
        $conds = [
            'order_id' => $param['order_id'],
        ];
        $fields = [
            'shipping_partername' => $param['shipping_partername'],
            'shipping_no' => $param['shipping_no'],
            'contact_phone' => $param['contact_phone'],
            'shipping_status' => Flag::SHIPPING_STATUS_SENDING,
        ];
        $ret = $this->obj->updateOrderByConds($conds, $fields);
        if (false === $ret) {
            return $this->getRes(Error::ERR_SYS);
        }
        return $this->getRes();
    }

    public function checkParam($param)
    {
        $rule = [
            'order_id' => 'require',
            'shipping_partername' => 'require',
            'shipping_no' => 'require',
            'contact_phone' => 'require',
        ];
        $msg = [
            'order_id.require' => '订单号码必须',
            'shipping_partername.require' => '物流配送方必填',
            'shipping_no.require' => '物流单号必填',
            'contact_phone.require' => '联系电话必填',
        ];

        $validate = new \think\Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

}